package com.zhuoyue.biz.core.command.impl.box;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.CmdParams;
import com.zhuoyue.biz.core.command.model.ControlBoxCmd;
import com.zhuoyue.biz.core.command.model.PanelCmd;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>Title:弱点控制盒 <／p>
 * <p>Description: <／p>
 * <p>Company: JiZi<／p>
 *
 * @author wjming
 * @date 2018年12月10日
 */
public class WeakCurrentControlBox0Faa0203 {
    /**
     * 配置区域信息参数
     */
    private static final String SET_MODEL = "0201${gatewayMac}${deviceMac}03EF1401${motorTimeParameters}";
    private static final String MOTOR_TIME_PARAMETER = "motorTimeParameter";
    public static String getCommand(String controlParam) {

        Map<String, String> map = (Map) JSONObject.parse(controlParam);

        String cmd = "";

        String controlType = map.get("controlType");
        if (StringUtils.isEmpty(controlType)) {
            Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
            if (StringUtils.isNotEmpty(actionsMap.get(MOTOR_TIME_PARAMETER))) {
                cmd = getRadarontrolCmd(map);
                return cmd;
            }
            return null;
        }

        if (controlType.equals(CmdParams.ControlType.CONTROL_TYPE_DIRECT)) {
            cmd = getDirectControlCmd(map);
        }

        return cmd;
    }


    /**
     * 获取弱电控制盒直接控制命令
     *
     * @param map
     * @return
     */
    private static String getDirectControlCmd(Map<String, String> map) {
        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String controlState = CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("controlState")));
        return new PanelCmd(map.get("gatewayMac"), map.get("deviceMac"), CmdParams.Od0Faa.WAY_1, controlState).getCmd(CmdParams.CmdModel.PANEL_0FAA);
    }

    private static String getRadarontrolCmd(Map<String, String> map) {
        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String motorTimeParameters = CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get(MOTOR_TIME_PARAMETER)));
        return new ControlBoxCmd(map.get("gatewayMac"), map.get("deviceMac"), motorTimeParameters).getCmd(SET_MODEL);
    }


}
